<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * CodeIgniter Order Library
 *
 * @package         CodeIgniter
 * @subpackage      Libraries
 * @category        Libraries
 * @author          Eason
 * @license         MIT
 */
class Order {
    protected $ci;
    private $_db;
    public function __construct() {
        $this->ci = & get_instance();
        $this->_db = $this->ci->load->database('default',true);
    }

    //创建订单
    public function createOrder($array,$prefix='wx',$table='cny_deposit'){
        $i = 1;
        $orderno = '';
        do {
            $orderno = $this->getOrderNo($prefix);
            $array['orderno'] =  $orderno;
            $rs = $this->_db->insert($table,$array);
            if(!$rs){
                $orderno = '';
            }
        }while(!$rs && $i++ < 5);
        return $orderno;
    }

    public function getOrder($orderno,$table='cny_deposit'){
        $query = $this->_db->select()->from($table)->where('orderno', $orderno)->limit(1)->get();
        if ($query->row()) {
            return $query->row_array();
        } else {
            return false;
        }
        return $rs;
    }

    public function updateOrder($array,$where,$table='cny_deposit'){
        $this->_db->where($where);
        if(!$this->_db->update($table, $array)){
            return false;
        }
        return true;
    }

    private function getOrderNo($prefix=1){
        $order_pre  =   $prefix;
        $orderno    =   $order_pre . date('ymdHis') . rand(1000,9999);
        return $orderno;
    }
    
}

?>